package com.trkj.controller.lzs;

import java.math.BigDecimal;
import java.sql.Timestamp;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trkj.pojo.ProjectTemplate;
import com.trkj.pojo.SalaryProject;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.service.lzs.SalarySableService;
import com.trkj.vo.Result2;
import com.trkj.vo.TemplateAddProjectVo;
import com.trkj.vo.TemplateRelevanceVo;
 @Controller
public class SalarySableController {
//薪资表模板
	//查询所有
	   @Autowired
	   private  SalarySableService salaryTemplateService;	
		@RequestMapping("lzs_findSalarySable")
		@ResponseBody
		public List<ProjectTemplate> SalarySablefind(){
			List<ProjectTemplate> list = salaryTemplateService.SalarySablefindAll();
		
			return list;
		}
		  //添加
				@RequestMapping("lzs_SalarySabletianjia")
				@ResponseBody
				public void SalarySabletianjia(Timestamp templateCreatordate,String templateName,String templatePerson,BigDecimal varsionNumber ){
					salaryTemplateService.wan( templateCreatordate, templateName, templatePerson,varsionNumber);
				}
			
				//查询shuoy
				@RequestMapping("lzs_SalarySablechaxun")
				@ResponseBody
				public  List<ProjectTemplate> Query(String text){
					
					return (List<ProjectTemplate>) salaryTemplateService.queryByName(text);
				}
				/*修改
				*/
				@RequestMapping("lzs_SalarySablexiugai")
				@ResponseBody  
				public  Result2  SalarySablexiugaixiugai(long templateId ,Timestamp templateCreatordate,String templateName,String templatePerson, BigDecimal varsionNumber) {
					try {
						salaryTemplateService.lzs_Edit_a(templateId, templateCreatordate, templateName, templatePerson, varsionNumber);
						return Result2.SUCCESS;
					} catch (Exception e) {
							e.printStackTrace();
						return Result2.failure(e.getMessage());
					}
					
				}
				
				@RequestMapping("lzs_SalarySablebj")
				@ResponseBody  
				 public ProjectTemplate chunq(Long templateId) {
			    	 return salaryTemplateService.chun_a(templateId);
			     }	
				//chaxun
				@GetMapping("SalaryProjectfindone")
				 public String chunq(Long templateId,Model m) {
					m.addAttribute("list",salaryTemplateService.SalaryProjectfindone(templateId));
			    	 return  "salarysable-add";	  
			    	 }
				//删除；通过id查出相应的一条数据
				@RequestMapping("hr_ra_form_SalarySable")
				@ResponseBody
				public boolean hr_ra_form_SalarySable(Long templateId){
					 return salaryTemplateService.dltRa(templateId);
				}
				
				
				
				
				
				/**
				 * 进入模板信息页面
				 * @param templateId
				 * @param m
				 * @return
				 */
				@GetMapping("findOneTemplate")
				public String findOneTemplate(Long templateId,Model m){
					try {
						m.addAttribute("list",salaryTemplateService.findOneTemplate(templateId));
					} catch (ServiceException e) {
						e.printStackTrace();
					}
					return "template_message";
				}
				
				/**
				 * 模板关联项目
				 * @param vo
				 */
				@RequestMapping("templateRelevance")
				@ResponseBody
				public void templateRelevance(TemplateRelevanceVo vo){
					try {
						salaryTemplateService.RelevanceProject(vo.getProjectId(), vo.getTemplateId());
					} catch (ServiceException e) {
						e.printStackTrace();
					}
				}
				
				/**
				 * 查询模板关联的项目
				 * @param templateId
				 * @return
				 * @throws ServiceException 
				 */
				@GetMapping("queryRelevanceProject")
				@ResponseBody
				public List<SalaryProject> queryRelevanceProject(Long templateId) throws ServiceException{
					return salaryTemplateService.queryRelevanceProject(templateId);
				}
				
				/**
				 * 模板新增项目
				 * @param templateId
				 * @param salaryProject
				 * @param httpSession
				 */
				@PostMapping("templateAddProject")
				@ResponseBody
				public void TemplateAddProject(TemplateAddProjectVo vo,HttpSession httpSession){
					try {
						StaffInformation staff = (StaffInformation) httpSession.getAttribute("staff");
						salaryTemplateService.TemplateAddProject(vo.getTemplateId(),vo.getSalaryProject(),staff.getStainName());
					} catch (ServiceException e) {
						e.printStackTrace();
					}
				}
			}